﻿@{
    Style.Require("NineSeas.Book.ChapterDropDown");
    Script.Require("NineSeas.PopupPanel");    

    bool isEdit = Model.IsEdit != null && Model.IsEdit;
    
    dynamic selectedChapter = null;
    if (Model.SelectedChapter != null) {
        selectedChapter = Model.SelectedChapter;
    } else if (Model.Chapters != null && Model.Chapters.Count > 0) {
        if (Model.SelectedChapterIndex == null || Model.SelectedChapterIndex >= Model.Chapters.Count) {
            selectedChapter = Model.Chapter[0];
        } else {
            selectedChapter = Model.Chapter[Model.SelectedChapterIndex];
        }
    }
    var height = 0;
    if (Model.Chapters != null && Model.Chapters.Count > 10) {
        height = 326;

        if (Model.CreateLink != null && !String.IsNullOrWhiteSpace(Model.CreateLink))
        {
            height = 358;
        }
    }
    
    
}
<div class="chapter-dropdown">
    <div class="dropdown-button"></div>
    <ul @(height > 0 ? "style=height:" + height + "px" : "")>
        @if (Model.Chapters != null && Model.Chapters.Count > 0) {
            foreach (dynamic chapter in Model.Chapters)
            {
                <li>
                    <a href="@(isEdit ?
                            Url.Action("Edit", "Chapter", new { id = chapter.Id })
                            : @Url.Action("Chapter", "BookView", new { name = Model.Book.NameOnUrl, chapterIndex = chapter.SortOrder }))">
                        @(!String.IsNullOrWhiteSpace(chapter.Title) ? (chapter.SortOrder + 1) + ". " + chapter.Title : T("Chapter").Text + " " + (chapter.SortOrder + 1))
                    </a>
                </li>
            }
        }

        @if (Model.CreateLink != null && !String.IsNullOrWhiteSpace(Model.CreateLink)) {
            <li>
                <i><a href="@Model.CreateLink">@T("Create new chapter...")</a></i>
            </li>
        }
    </ul>
    @if (selectedChapter != null) {
    <div class="chapter-dropdown-selected"> 
        @(!String.IsNullOrWhiteSpace(selectedChapter.Title) ? (selectedChapter.SortOrder + 1) + ". " + selectedChapter.Title : T("Chapter").Text + " " + (selectedChapter.SortOrder + 1))
    </div>
    }
</div>
@using (Script.Foot()) {
<script type="text/javascript">
    //<![CDATA[
    $(function () {
        $.popupPanel(".dropdown-button, .chapter-dropdown-selected", ".chapter-dropdown ul");
    });
    //]]>
</script>
}